package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.atguigu.commonutils.R;
import com.atguigu.vod.service.VodService;
import com.atguigu.vod.utils.ConstantVodUtils;
import com.atguigu.vod.utils.InitVodClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @program: guli_parent
 * @description:
 * @author: xuejingjing
 * @create: 2020-12-21 03:51
 **/

@RestController
@RequestMapping("/eduvod/video")
public class VodController {

    @Autowired
    private VodService vodService;

    //上传视频到阿里云
    @PostMapping("uploadAlVideo")
    //得到上传文件
    public R uploadAlVideo(MultipartFile file){
       String videoId = vodService.uploadAlVideo(file);
       return R.ok().data("videoId",videoId);
    }

    //根据视频Id删除阿里云视频
    @DeleteMapping("removeAiVideo/{id}")
    public R removeAliVideo(@PathVariable String id) {

        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);

            //创建删除视频request对象
            DeleteVideoRequest deleteVideoRequest = new DeleteVideoRequest();

            //向request设置视频id
            deleteVideoRequest.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(deleteVideoRequest);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return R.ok();
    }

    //删除多个阿里云视频的方法
    //参数多个视频id List  videoList
    @DeleteMapping("delete-batch")
    public R deleteBatch(@RequestParam("videoIdList") List videoIdList){

        vodService.removeMoreAliVideos(videoIdList);
        return R.ok();
    }


}
